import { useRequestURL } from '#app'

export const useUrlParams = () => {
  const requestURL = useRequestURL()

  // 获取 home 参数（不包含 name 参数）
  const getHomeParams = () => {
    const queryParams = Object.fromEntries(requestURL.searchParams)
    delete queryParams.name
    delete queryParams.category
    delete queryParams.subCategory
    delete queryParams.app
    delete queryParams.searchKey
    delete queryParams.cost
    return queryParams
  }

  // 获取所有查询参数
  const getAllParams = () => {
    return Object.fromEntries(requestURL.searchParams)
  }

  // 获取指定参数
  const getParam = (key: string) => {
    return requestURL.searchParams.get(key)
  }

  // 检查参数是否存在
  const hasParam = (key: string) => {
    return requestURL.searchParams.has(key)
  }

  // 构建查询字符串
  const buildQueryString = (params: Record<string, any>) => {
    return new URLSearchParams(params).toString()
  }

  // 构建完整URL
  const buildUrl = (path: string, params: Record<string, any> = {}) => {
    const queryString = buildQueryString(params)
    return queryString ? `${path}?${queryString}` : path
  }

  return {
    requestURL,
    getHomeParams,
    getAllParams,
    getParam,
    hasParam,
    buildQueryString,
    buildUrl
  }
}
